<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="" prop="uid">
      <el-input v-model="dataForm.uid" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="认证状态：1认证成功，0认证中，-1认证失败" prop="status">
      <el-input v-model="dataForm.status" placeholder="认证状态：1认证成功，0认证中，-1认证失败"></el-input>
    </el-form-item>
    <el-form-item label="认证称号" prop="name">
      <el-input v-model="dataForm.name" placeholder="认证称号"></el-input>
    </el-form-item>
    <el-form-item label="认证名称前缀" prop="nameSuffix">
      <el-input v-model="dataForm.nameSuffix" placeholder="认证名称前缀"></el-input>
    </el-form-item>
    <el-form-item label="手机号" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="手机号"></el-input>
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input v-model="dataForm.email" placeholder="邮箱"></el-input>
    </el-form-item>
    <el-form-item label="所在省份" prop="province">
      <el-input v-model="dataForm.province" placeholder="所在省份"></el-input>
    </el-form-item>
    <el-form-item label="所在城市" prop="city">
      <el-input v-model="dataForm.city" placeholder="所在城市"></el-input>
    </el-form-item>
    <el-form-item label="身份证图片url" prop="idNumber">
      <el-input v-model="dataForm.idNumber" placeholder="身份证图片url"></el-input>
    </el-form-item>
    <el-form-item label="站外链接" prop="link">
      <el-input v-model="dataForm.link" placeholder="站外链接"></el-input>
    </el-form-item>
    <el-form-item label="申请日期" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="申请日期"></el-input>
    </el-form-item>
    <el-form-item label="更新日期" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新日期"></el-input>
    </el-form-item>
    <el-form-item label="过期时间" prop="expiresTime">
      <el-input v-model="dataForm.expiresTime" placeholder="过期时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          uid: '',
          status: '',
          name: '',
          nameSuffix: '',
          phone: '',
          email: '',
          province: '',
          city: '',
          idNumber: '',
          link: '',
          createTime: '',
          updateTime: '',
          expiresTime: ''
        },
        dataRule: {
          uid: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '认证状态：1认证成功，0认证中，-1认证失败不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '认证称号不能为空', trigger: 'blur' }
          ],
          nameSuffix: [
            { required: true, message: '认证名称前缀不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '手机号不能为空', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '邮箱不能为空', trigger: 'blur' }
          ],
          province: [
            { required: true, message: '所在省份不能为空', trigger: 'blur' }
          ],
          city: [
            { required: true, message: '所在城市不能为空', trigger: 'blur' }
          ],
          idNumber: [
            { required: true, message: '身份证图片url不能为空', trigger: 'blur' }
          ],
          link: [
            { required: true, message: '站外链接不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '申请日期不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新日期不能为空', trigger: 'blur' }
          ],
          expiresTime: [
            { required: true, message: '过期时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/common/userauthentication/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.uid = data.userAuthentication.uid
                this.dataForm.status = data.userAuthentication.status
                this.dataForm.name = data.userAuthentication.name
                this.dataForm.nameSuffix = data.userAuthentication.nameSuffix
                this.dataForm.phone = data.userAuthentication.phone
                this.dataForm.email = data.userAuthentication.email
                this.dataForm.province = data.userAuthentication.province
                this.dataForm.city = data.userAuthentication.city
                this.dataForm.idNumber = data.userAuthentication.idNumber
                this.dataForm.link = data.userAuthentication.link
                this.dataForm.createTime = data.userAuthentication.createTime
                this.dataForm.updateTime = data.userAuthentication.updateTime
                this.dataForm.expiresTime = data.userAuthentication.expiresTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/common/userauthentication/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'uid': this.dataForm.uid,
                'status': this.dataForm.status,
                'name': this.dataForm.name,
                'nameSuffix': this.dataForm.nameSuffix,
                'phone': this.dataForm.phone,
                'email': this.dataForm.email,
                'province': this.dataForm.province,
                'city': this.dataForm.city,
                'idNumber': this.dataForm.idNumber,
                'link': this.dataForm.link,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'expiresTime': this.dataForm.expiresTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
